package com.heima.behavior.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.behavior.dto.ReadBehaviorDto;
import com.heima.behavior.entity.ApReadBehavior;
import com.heima.behavior.mapper.ApReadBehaviorMapper;
import com.heima.behavior.service.IApReadBehaviorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.ResponseResult;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * APP阅读行为表 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2022-01-21
 */
@Service
public class ApReadBehaviorServiceImpl extends ServiceImpl<ApReadBehaviorMapper, ApReadBehavior> implements IApReadBehaviorService {

    /**
     * 阅读
     * @param dto
     * @return
     */
    @Override
    public ResponseResult readArticle(ReadBehaviorDto dto) {
        LambdaQueryWrapper<ApReadBehavior> wrapper = new LambdaQueryWrapper<>();//创建查询条件
        //用户登录
        if (dto.getUserId() != null && dto.getArticleId() != null) {
            wrapper.eq(ApReadBehavior::getArticleId, dto.getArticleId());
            wrapper.eq(ApReadBehavior::getEntryId, dto.getUserId());
            ApReadBehavior behavior = this.getOne(wrapper);//查询
            if (behavior != null) {//更新
                int count = behavior.getCount() + 1;
                BeanUtils.copyProperties(dto, behavior);
                behavior.setCount(count);//更新阅读次数
                behavior.setUpdatedTime(new Date());//更新最近一次阅读时间
                this.updateById(behavior);
                return ResponseResult.okResult();
            }
            //保存
            ApReadBehavior readBehavior = new ApReadBehavior();
            BeanUtils.copyProperties(dto, readBehavior);
            readBehavior.setEntryId(dto.getUserId());//封装用户id
            readBehavior.setCount(1);//封装阅读次数 第一次1
            readBehavior.setCreatedTime(new Date());//封装创建时间4
            this.save(readBehavior);
            return ResponseResult.okResult();
        }
        //游客登录
        if (dto.getUserId()==null&&dto.getEquipmentId()!=null&& dto.getArticleId()!= null){
            wrapper.eq(ApReadBehavior::getArticleId, dto.getArticleId());
            wrapper.eq(ApReadBehavior::getEquipmentId, dto.getEquipmentId());
            ApReadBehavior behavior = this.getOne(wrapper);//查询
            //更新
            if (behavior != null) {
                int count = behavior.getCount() + 1;
                BeanUtils.copyProperties(dto, behavior);
                behavior.setCount(count);//更新阅读次数
                behavior.setUpdatedTime(new Date());//更新最近一次阅读时间
                this.updateById(behavior);
                return ResponseResult.okResult();
            }
            //保存
            ApReadBehavior readBehavior = new ApReadBehavior();
            BeanUtils.copyProperties(dto, readBehavior);
            readBehavior.setCount(1);//封装阅读次数 第一次1
            readBehavior.setCreatedTime(new Date());//封装创建时间
            this.save(readBehavior);
            return ResponseResult.okResult();
        }
        return ResponseResult.okResult();
    }
}
